cmake_minimum_required(VERSION 3.5)

project(ukuiwaylandscanner)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

find_package(QT NAMES Qt5 Qt6 REQUIRED COMPONENTS Core)
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Core)

remove_definitions(-DQT_NO_CAST_FROM_ASCII)
add_executable(ukuiwaylandscanner ukuiwaylandscanner.cpp)
target_link_libraries(ukuiwaylandscanner Qt${QT_VERSION_MAJOR}::Core)

add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/wayland.cpp 
    COMMAND ukuiwaylandscanner /usr/share/wayland/wayland.xml --output-dir=${CMAKE_CURRENT_BINARY_DIR}
    DEPENDS /usr/share/wayland/wayland.xml
)

add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/ukui-wayland-client-core.cpp
    COMMAND mv ${CMAKE_CURRENT_BINARY_DIR}/wayland.cpp ${CMAKE_CURRENT_BINARY_DIR}/ukui-wayland-client-core.cpp
    COMMAND mv ${CMAKE_CURRENT_BINARY_DIR}/wayland.h ${CMAKE_CURRENT_BINARY_DIR}/ukui-wayland-client-core.h
    COMMAND sed -i "s/#include \\\"wayland.h\\\"/#include \\\"ukui-wayland-client-core.h\\\"/g" ${CMAKE_CURRENT_BINARY_DIR}/ukui-wayland-client-core.cpp
    COMMAND sed -i "\"s/#include \\\"wayland-wayland-client-protocol.h\\\"/#include <wayland-client.h>/g\"" ${CMAKE_CURRENT_BINARY_DIR}/ukui-wayland-client-core.h
    COMMAND sed -i "\"/#include <ukui-wayland-client.h>/d\"" ${CMAKE_CURRENT_BINARY_DIR}/ukui-wayland-client-core.h
    DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/wayland.cpp
)

include_directories(${CMAKE_CURRENT_BINARY_DIR})

add_library(ukui-wayland-client SHARED ukui-wayland-client.cpp ukui-wayland-client-core.cpp)
target_link_libraries(ukui-wayland-client PUBLIC wayland-client)

install(TARGETS ukui-wayland-client
    EXPORT UkuiWaylandClientTargets
    LIBRARY DESTINATION lib
    INCLUDES DESTINATION include
)

install(EXPORT UkuiWaylandClientTargets
    FILE UkuiWaylandClientTargets.cmake
    DESTINATION lib/cmake/UkuiWaylandClient
)

install (FILES
    ${CMAKE_CURRENT_BINARY_DIR}/ukui-wayland-client-core.h
    ukui-wayland-client.h
    DESTINATION include
)

install(TARGETS ukuiwaylandscanner RUNTIME DESTINATION bin)
install(FILES ${CMAKE_SOURCE_DIR}/UkuiWaylandClientConfig.cmake DESTINATION lib/cmake/UkuiWaylandClient)